var app = getApp();

function wxLogin() {
	wx.login({
		success: res => {
			let loginCode = res.code //换取openid的code
			if (loginCode) {
				// 通过code获取unionid
				uni.request({
					url: app.globalData.apiPath + '/weixin/tenantLogin',
					method: 'POST',
					data: {
						loginCode: loginCode
					}
				}).then((response) => {
					let [error, res] = response;
					console.log("res.data: "+JSON.stringify(res.data))
					if (res.data.msg != "操作成功") {
						return res.data.msg;
					}
					
					if (res.data.token) {
						uni.setStorageSync("tenant_unionid", res.data.tenant_unionid)
						uni.setStorageSync("tenant_token", res.data.token)
					} else {
						return '获取登录token失败，请联系管理员';
					}
				}).catch((error) => {
					console.log(error)
				})
			} else {
				return '获取微信openid失败，请联系管理员';
			}
		}
	})
}

module.exports = {
	wxLogin: wxLogin
};